<?php
/*
 * Created on
 * Date: 2007-12-4
 * Author: William Yang
 */
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(DB_SESSION_DIR_PATH."/db_obj_defs.php");
require_once(TRANSACTIONS_DIR_PATH."/trans_common.php");

class CounterManager extends Counter {
	public function register() {
		$id = parent::register(CONFIGURATION_DIR_PATH."/create_counter.xml",
								FALSE);
		return $id;
	}
	
	public static function update($c_id, $delta = 1) {
		$operator = new DatabaseOperator();
		$operator->config(CONFIGURATION_DIR_PATH."/counter_table.xml");
		
		$c_mgr = new CounterManager($c_id, new DeltaValue($delta));
		$info = $c_mgr->toArray();
		
		if (count($info)) {
			$condList = new SqlCondList();
			$condList->add_condition(new SqlCondition('uid', SqlCondition::EQUAL, $c_id));
			$stat = $operator->update($info, $condList);
			if(!DbError::is_error($stat)) {
				return TRUE;
			} else {
				return FALSE;
			}
		} else {
			return TRUE;
		}
	}
	
	public function query_by_uids($uid) {
		/*
		 * Query information by the UID specified by $uid.
		 */
		$rows = parent::query_by_uids(CONFIGURATION_DIR_PATH."/counter_table.xml", $uid);
		
		if ($rows === NULL) {
			return NULL;
		} else {
			$ret = array();
			$b_delegate = new Counter();
			foreach ($rows as $row) {
				$tmp = $b_delegate->array2copy($row);
				$ret[] = $tmp;
			}
			return $ret;
		}
	}
}
?>
